package com.yanqu.road.dao.impl.activity.rankunionwar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.rankunionwar.data.CrossRankUnionWarMemberData;
import com.yanqu.road.entity.activity.rankunionwar.data. CrossRankUnionWarUnionData;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossRankUnionWarUnionDataDaoImpl extends TempDataObjectDao<CrossRankUnionWarUnionData> {

    public static String getDataSql = "select * from t_c_activity_rank_union_war_union where activityId=?";
    public  Map<String, CrossRankUnionWarUnionData> getData(int activityId) {
        Map<String, CrossRankUnionWarUnionData> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getDataSql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                 CrossRankUnionWarUnionData data = parse(rs);
                dataMap.put(data.getUnionId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String insertUserDataSql = "insert into t_c_activity_rank_union_war_union(" +
            " activityId,unionId,loseTimes,winTimes,die,donateNum,donateData,unionBaseInfo,memberData,opUserIdData,updatePatronsTime,score) " +
            " values (?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData( CrossRankUnionWarUnionData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLoseTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWinTimes()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isDie()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDonateNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getDonateMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getMemberMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getOpUserIdMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdatePatronsTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getScore()));

        if (getDb().executeNoneQuery(insertUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUserDataSql = "update t_c_activity_rank_union_war_union set " +
            " loseTimes=?,winTimes=?,die=?,donateNum=?,donateData=?,unionBaseInfo=?, memberData=?,opUserIdData=?,updatePatronsTime=?,score=?" +
            " where activityId=? AND unionId=?";
    @Override
    protected boolean updateData( CrossRankUnionWarUnionData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getLoseTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWinTimes()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isDie()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDonateNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getDonateMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getMemberMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getOpUserIdMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdatePatronsTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionId()));
        if (getDb().executeNoneQuery(updateUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected  CrossRankUnionWarUnionData parseData(ResultSet rs) throws SQLException {
         CrossRankUnionWarUnionData data = new  CrossRankUnionWarUnionData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUnionId(rs.getString("unionId"));
        data.setLoseTimes(rs.getInt("loseTimes"));
        data.setWinTimes(rs.getInt("winTimes"));
        data.setDie(rs.getBoolean("die"));
        data.setDonateNum(rs.getInt("donateNum"));
        data.setUpdatePatronsTime(rs.getLong("updatePatronsTime"));
        data.setScore(rs.getInt("score"));

        String donateDataStr  = rs.getString("donateData");
        if(!StringUtils.isNullOrEmpty(donateDataStr)){
            Map<Integer, Map<Integer, Integer>> result = JSON.parseObject(donateDataStr, new TypeReference<Map<Integer, Map<Integer, Integer>>>() {});
            if(result != null){
                data.setDonateMap(result);
            }
        }
        String unionBaseInfoStr  = rs.getString("unionBaseInfo");
        UnionBaseInfo unionBaseInfo = JSON.parseObject(unionBaseInfoStr, new TypeReference<UnionBaseInfo>() {});
        if(null != unionBaseInfo){
            data.setUnionBaseInfo(unionBaseInfo);
        }

        String memberDataStr  = rs.getString("memberData");
        if(!StringUtils.isNullOrEmpty(donateDataStr)){
            Map<Long, CrossRankUnionWarMemberData> result = JSON.parseObject(memberDataStr, new TypeReference<Map<Long, CrossRankUnionWarMemberData>>() {});
            if(result != null){
                data.setMemberMap(result);
            }
        }
        String opUserIdDataStr  = rs.getString("opUserIdData");
        if(!StringUtils.isNullOrEmpty(donateDataStr)){
            Map<Integer, Map<Integer, List<Long>>> result = JSON.parseObject(opUserIdDataStr, new TypeReference<Map<Integer, Map<Integer, List<Long>>>>() {});
            if(result != null){
                data.setOpUserIdMap(result);
            }
        }
        return data;
    }

}
